﻿using System.Windows.Controls;

namespace ibba.src.sl.recursos
{
    /// <summary>
    /// HotFix para controle AutoComplete
    /// Erro: Corrigi problema de seleção do item quando existe valores com o mesmo texto
    /// </summary>
    public class AutoCompleteBoxFix: AutoCompleteBox
    {
        private object _selectedItemFix;
        public object SelectedItemFix
        {
            get
            {
                return (_selectedItemFix != null) ? _selectedItemFix : SelectedItem;                
            }
            set
            {
                _selectedItemFix = value;
            }
        }

        protected override void OnDropDownClosing(RoutedPropertyChangingEventArgs<bool> e)
        {
            base.OnDropDownClosing(e);
            SelectedItemFix = SelectionAdapter.SelectedItem;
        }

        protected override void OnTextChanged(System.Windows.RoutedEventArgs e)
        {
            base.OnTextChanged(e);
            if (SelectionAdapter.SelectedItem == null)
                _selectedItemFix = null; 
        }
        protected override void OnSelectionChanged(SelectionChangedEventArgs e)
        {
            base.OnSelectionChanged(e);

            if (SelectionAdapter.SelectedItem == null)
                _selectedItemFix = null; 
        }

        //protected override void OnLostFocus(System.Windows.RoutedEventArgs e)
        //{
        //    base.OnLostFocus(e);

        //    if (SelectedItem == null)
        //        _selectedItemFix = null;
        //}
    }
}
